package utils

import java.io.File
import org.apache.commons.io.FileUtils
import shapeless.{HList, LabelledGeneric, Lazy}

import java.text.SimpleDateFormat
import java.util.Date
import scala.concurrent.{Await, Future}
import scala.concurrent.duration.Duration
import implicits.Implicits._

object Utils {

  val dbName = "live_boost_database"
  val projectName = "live_boost"
  val idStr = "boost_id"
  val windowsPath = s"D:\\${dbName}"
  val playPath = new File("../").getAbsolutePath
  val linuxPath = playPath + s"/${dbName}"
  val isWindows = {
    if (new File(windowsPath).exists()) true else false
  }
  val errorClass = "error"
  val successClass = "text-success"

  val orthoMclStr = "orthoMcl"
  val mummerStr = "mummer"
  val ardbStr = "ardb"
  val cazyStr = "cazy"


  val path = {
    if (new File(windowsPath).exists()) windowsPath else linuxPath
  }
  val dataFile = new File(path, "data")
  val proteinDir = new File(dataFile, "protein")

  val binPath = new File(path, "bin")
  val anno = new File(binPath, "Anno")
  val orthoMcl = new File(binPath, "ORTHOMCLV1.4")
  val pipeLine = new File("/mnt/sdb/linait/pipeline/MicroGenome_pipeline/MicroGenome_pipeline_v3.0")
  val mummer = new File("/mnt/sdb/linait/tools/quickmerge/MUMmer3.23/")
  val blastFile = new File(binPath, "ncbi-blast-2.6.0+/bin/blastn")
  val blastpFile = new File(binPath, "ncbi-blast-2.6.0+/bin/blastp")
  val blastxFile = new File(binPath, "ncbi-blast-2.6.0+/bin/blastx")
  val blast2HtmlFile = new File(binPath, "blast2html-82b8c9722996/blast2html.py")
  val rPath = {
    val rPath = s"C:\\workspaceForIDEA\\live_boost_scala_js\\server\\rScripts"
    val linuxRPath = linuxPath + "/rScripts"
    val finalPath = if (new File(rPath).exists()) rPath else linuxRPath
    new File(finalPath)
  }
  val userDir = new File(path, "user")

  val windowsTestDir = new File("G:\\temp")
  val linuxTestDir = new File(playPath, "workspace")
  val testDir = if (windowsTestDir.exists()) windowsTestDir else linuxTestDir
  val dataDir = new File(path, "data")

  case class Sender(nick: String, host: String, email: String, password: String)

  def getInfoByXlsxFile(xlsxFile: File, sheetIndex: Int = 0) = {
    val lines = xlsxFile.xlsxLines(sheetIndex)
    getInfoByLines(lines)
  }

  def getInfoByLines(lines: List[List[String]]) = {
    val trueLines = lines.filter { x =>
      !x.head.startsWith("#")
    }
    val columnNames = trueLines.head
    val array = trueLines.drop(1).filter(x => x.size == columnNames.size).map { tmpColumns =>
      val columns = tmpColumns.map { x =>
        x.replaceAll("^\"", "").replaceAll("\"$", "")
      }
      columnNames.zip(columns).toMap
    }
    (columnNames, array)
  }

  def execFuture[T](f: Future[T]): T = {
    Await.result(f, Duration.Inf)
  }

  def isDouble(value: String): Boolean = {
    try {
      value.toDouble
    } catch {
      case _: Exception =>
        return false
    }
    true
  }

  def deleteDirectory(direcotry: File) = {
    try {
      FileUtils.deleteDirectory(direcotry)
    } catch {
      case _ =>
    }
  }

  def getTime(startTime: Long) = {
    val endTime = System.currentTimeMillis()
    (endTime - startTime) / 1000.0
  }

}
